from itertools import chain
import datetime

def model_to_dict(instance, fields=None, exclude=None):
    opts = instance._meta
    data = {}
    for f in chain(opts.concrete_fields, opts.private_fields, opts.many_to_many):
        # if not getattr(f, 'editable', False):
        #     continue
        if fields and f.name not in fields:
            continue
        if exclude and f.name in exclude:
            continue
        value = f.value_from_object(instance)
        if type(value) == datetime.datetime:
            value = value.strftime("%Y-%m-%d %H:%M:%S")
        if type(value) == datetime.date:
            value = value.strftime("%Y-%m-%d")

        data[f.name] = value
    return data

class FormatError:

    @property
    def error_msg(self):
        result = ""
        for k, v in self.errors.get_json_data().items():
            message = v[0].get("message")
            msg = "%s_%s" % (k, message)
            result += msg
        return result